package com.itheima.web.aop;

import com.itheima.web.MyInterface.Aop;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;

@Component
@Aspect
public class UesrLog {


    @Pointcut("execution(* com.itheima.web.controller.UserController.*(..))")
    public void pointCut(){}

    @Before("pointCut()")
    public void doBefore(JoinPoint joinPoint){
        System.out.println("AOP Before Advice...");
    }

    @After("pointCut()")
    public void doAfter(JoinPoint joinPoint){
        System.out.println("AOP After Advice...");
    }

    //环绕通知,需要显示调用,否则不执行目标方法,ProceedingJoinPoint必须在第一个形参位置上
    @Around("execution(* com.itheima.web.controller.UserController.*(..))")
    public Object around(ProceedingJoinPoint pj) throws Throwable {
        Class<?> aClass = Class.forName("com.itheima.web.controller.UserController");
        Method[] declaredMethods = aClass.getDeclaredMethods();
        for (Method declaredMethod : declaredMethods) {

            Aop aop = declaredMethod.getAnnotation(Aop.class);

            System.out.println(aop.value());
        }

        System.out.println("环绕通知---前执行");
        Object proceed = pj.proceed();
        System.out.println("环绕通知---后执行");
        return proceed;
    }

}
